package com.kitty.game.equip.handler;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.utils.AsktaoUtil;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.HashSet;
import java.util.Set;

import static com.kitty.game.utils.Const.*;
import com.kitty.game.equip.handler.UpgradeHandler;

@Component("SplitJewelryHandler")
public class SplitJewelryHandler extends UpgradeHandler {
    private static final String ERROR_RESOURCES = "请放入正确的装备或材料。";
    private static final String SUCCESS = "恭喜你，花费了{0}文钱#n，成功分解了#R{1}#n件首饰，获得了#R{2}#n点首饰精华。";

    @Override
    @SuppressWarnings("unchecked")
    public String doUpgrade(Role role, Object... params) {
        int needCash = (int) params[1];
        int jewelryEssence = (int) params[2];
        Set<Integer> equipIds = (Set<Integer>) params[0];

        roleService.subtractMoney(role, needCash);
        equipIds.forEach(equipId -> {
            RoleEquip roleEquip = equipService.getEquipById(role, equipId);
            equipService.delRoleEquip(roleEquip,role);
        });
        roleService.addJewelryEssence(role, jewelryEssence);
        return MessageFormat.format(SUCCESS, AsktaoUtil.getMoneyFormat(needCash), equipIds.size(), jewelryEssence);
    }

    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        String[] posArr = param.split("\\|");
        int needCash = 0;
        int jewelryEssence = 0;
        Set<Integer> equipIds = new HashSet<>();
        for (String pos : posArr) {
            RoleEquip roleEquip = equipService.getRoleEquipByPos(role, Integer.parseInt(pos));
            if (roleEquip == null) {
                return ERROR_RESOURCES;
            }
            if (!isRightPosition(roleEquip.queryEquipPos(false))) {
                return ERROR_RESOURCES;
            }
            equipIds.add(roleEquip.getId());
            needCash += needCash(roleEquip.queryLevel(), 0);
            jewelryEssence += getJewelryEssence(roleEquip.queryLevel());
        }
        if (role.getMoney() < needCash) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, MONEY_NAME);
        }
        if (equipIds.size() <= 0) {
            return ERROR_RESOURCES;
        }

        return doUpgrade(role, equipIds, needCash, jewelryEssence);
    }

    @Override
    public String checkResource(Object... objects) {
        return null;
    }

    @Override
    public int needCash(short level, int pos) {
        int needMoney = 0;
        if (level == 80) {
            needMoney = 350000;
        } else if (level == 90) {
            needMoney = 600000;
        } else if (level == 100) {
            needMoney = 900000;
        } else if (level == 110) {
            needMoney = 1200000;
        } else if (level == 120) {
            needMoney = 1500000;
        }
        return needMoney;
    }

    @Override
    public String getUpdateTypeStr() {
        return "splite_jewelry";
    }

    /**
     * 判断打造的物品装备的部位是否符合条件
     */
    @Override
    boolean isRightPosition(byte equipPos) {
        return equipPos == NECKLACE || equipPos == BALDRIC || equipPos == LEFT_WRIST;
    }

}
